package com.shixun.dao;

import com.opensymphony.xwork2.ActionContext;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.ApplicationMap;
import org.apache.struts2.dispatcher.SessionMap;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import sun.misc.BASE64Encoder;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Random;


/**
 * Created by haiyan on 2016/12/16.
 */
public class BaseDao {
    @Resource
    private SessionFactory sessionFactory;

    /**
     * 开启事物
     *
     * @return
     */
    public Session getSession() {
        return sessionFactory.getCurrentSession();
    }

    /**
     * 添加
     * @param oj
     */
    public void userAdd(Object oj) {
        Session session = getSession();
        session.save(oj);
    }

    /**
     * 删除
     */
    public void deleteId(String sql){
        Session session = getSession();
        Query query = session.createQuery(sql);
        query.executeUpdate();
//        Query query = session.createQuery("DELETE FROM FileEntity WHERE id = '" + object + "'");
//        query.executeUpdate();
//        session.delete(object);
    }

    /**
     * 查找用户
     *
     * @param sql
     * @return
     */
    public List<Object> userSelect(String sql) {
        Session session = getSession();
        Query query = session.createQuery(sql);
        List<Object> list = query.list();
        return list;
    }

    /**
     * 给一个字符串加一
     * rerturn 字符串
     */
    public static String returnAddyi(String string){    //好牛逼的异常   《跳过此段继续执行》
        try {
            int i = Integer.parseInt(string);
            i++;
            String str = Integer.toString(i);
            return str;
        }catch (Exception e){
            System.out.println("调用'returnAddyi'方法时，传进来的字符串为空");
        }
        return null;
    }

    /**
     * 生成随机字符串
     */
    public static String suijiString(int length) {
        String string = "1234567890qwertyuioplkjhgfdsazxcvbnm";
        Random random = new Random();
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int num = random.nextInt(string.length());
            stringBuffer.append(string.charAt(num));
        }
        return stringBuffer.toString();
    }

    /**
     * 获取request
     */
    public static HttpServletRequest getRequest() {
        return (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
    }

    /**
     * 获取response
     */
    public static HttpServletResponse getResponse() {
        return (HttpServletResponse) ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE);
    }

    /**
     * 获取session的方法
     */
    public static SessionMap<String, Object> getSessions() {
        return (SessionMap<String, Object>) ActionContext.getContext().getSession();
    }

    /**
     * 获取Appliction的方法
     */
    public static ApplicationMap getApplication() {
        return (ApplicationMap) ActionContext.getContext().getApplication();
    }
    /**
     * 将s按MD5算法 进行加密
     * @param s 加密字符串
     */
    public static String userPasswordMD5(String s) {
        byte abyte[];
        try {
            MessageDigest messagedigest = MessageDigest.getInstance("MD5");
            messagedigest.update(s.getBytes("UTF8"));
            abyte = messagedigest.digest();
            return (new BASE64Encoder()).encode(abyte);
        } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
            System.out.println("--encryptPasswordMD5-->" + e.toString());
        }
        return null;
    }
}
